package com.ltkj.baseplatform.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ltkj.baseplatform.domain.SerCollectData;
import com.ltkj.baseplatform.domain.bo.ArtCollectionQueryBo;
import com.ltkj.baseplatform.mapper.*;
import com.ltkj.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltkj.baseplatform.domain.ArtCollection;
import com.ltkj.baseplatform.service.IArtCollectionService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 回收站Service业务层处理
 * 
 * @author liupf
 * @date 2023-08-28
 */
@Service
public class ArtCollectionServiceImpl implements IArtCollectionService 
{
    @Autowired
    private ArtCollectionMapper artCollectionMapper;

    @Autowired
    private ArtCollectionBusinessMapper artCollectionBusinessMapper;

    @Autowired
    private ArtChannelBusinessMapper artChannelBusinessMapper;

    @Autowired
    private ArtContentBusinessMapper artContentBusinessMapper;

    @Autowired
    private SerCollectDataMapper serCollectDataMapper;

    /**
     * 查询回收站
     * 
     * @param id 回收站主键
     * @return 回收站
     */
    @Override
    public ArtCollection selectArtCollectionById(Long id)
    {
        return artCollectionMapper.selectArtCollectionById(id);
    }

    /**
     * 查询回收站列表
     * 
     * @param artCollection 回收站
     * @return 回收站
     */
    @Override
    public List<ArtCollection> selectArtCollectionList(ArtCollection artCollection)
    {
        return artCollectionMapper.selectArtCollectionList(artCollection);
    }
    @Override
    public List<ArtCollection> selectArtCollectionList(ArtCollectionQueryBo artCollectionQueryBo)
    {
        return artCollectionBusinessMapper.selectArtCollectionList(artCollectionQueryBo);
    }

    /**
     * 新增回收站
     * 
     * @param artCollection 回收站
     * @return 结果
     */
    @Override
    public int insertArtCollection(ArtCollection artCollection)
    {
        artCollection.setCreateTime(DateUtils.getNowDate());
        return artCollectionMapper.insertArtCollection(artCollection);
    }

    /**
     * 修改回收站
     * 
     * @param artCollection 回收站
     * @return 结果
     */
    @Override
    public int updateArtCollection(ArtCollection artCollection)
    {
        artCollection.setUpdateTime(DateUtils.getNowDate());
        return artCollectionMapper.updateArtCollection(artCollection);
    }

    /**
     * 批量删除回收站
     * 
     * @param ids 需要删除的回收站主键
     * @return 结果
     */
    @Override
    public int deleteArtCollectionByIds(Long[] ids)
    {
        //这里使用物理删除，
        int i = artCollectionMapper.deleteArtCollectionByIds(ids);
        if(i > 0){
            SerCollectData serCollectData = null;
            //批量删除的数据不会很多
            for (Long id : ids) {
                serCollectData = new SerCollectData();
                serCollectData.setBizId(id);
                serCollectData.setIsDel("1");
                serCollectData.setTypeCode("article");
                serCollectDataMapper.updateDelFlagByBizCode(serCollectData);
            }
        }

        //逻辑删除
        //return artCollectionBusinessMapper.deleteArtCollectionByIds(ids);
        return i;
    }

    /**
     * 删除回收站信息
     * 
     * @param id 回收站主键
     * @return 结果
     */
    @Override
    public int deleteArtCollectionById(Long id)
    {
        return artCollectionMapper.deleteArtCollectionById(id);
    }
    @Transactional
    @Override
    public int revert(Long[] ids, List<ArtCollection> list) {
        int sum = artCollectionMapper.deleteArtCollectionByIds(ids);
        List<Long> channelIds = new ArrayList<>();//栏目id集合
        List<Long> contentIds = new ArrayList<>();//内容id集合
        for (ArtCollection collection : list) {
            //0栏目,1内容
            if("0".equals(collection.getType())){
                channelIds.add(collection.getContentId());
            }else if("1".equals(collection.getType())){
                contentIds.add(collection.getContentId());
            }
        }

        if(!CollectionUtils.isEmpty(channelIds)){
            artChannelBusinessMapper.revert(channelIds);
        }

        if(!CollectionUtils.isEmpty(contentIds)){
            artContentBusinessMapper.revert(contentIds);
            SerCollectData serCollectData = null;
            //还原数据（批量删除的数据不会很多）
            for (Long id : contentIds) {
                serCollectData = new SerCollectData();
                serCollectData.setBizId(id);
                serCollectData.setIsDel("0");
                serCollectData.setTypeCode("article");
                serCollectDataMapper.updateDelFlagByBizCode(serCollectData);
            }
        }
        return sum;
    }

    @Override
    public List<ArtCollection> selectCollectionListByIds(Long[] ids) {
        return artCollectionBusinessMapper.selectCollectionListByIds(ids);
    }
}
